﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.MaterialManagement
{
    public class TaskSeachHandler : IRequestHandler<TaskSeachCommon, Result>
    {
        private IBaseRepositoryGet<ProductionTasks> ProductionTasksRepository;
        public TaskSeachHandler(IBaseRepositoryGet<ProductionTasks> productionTasksRepository)
        {
            ProductionTasksRepository = productionTasksRepository;
        }

        public async Task<Result> Handle(TaskSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };
            string sql = "SELECT * FROM ProductionTasks";
            var list = ProductionTasksRepository.GetAll(sql, default).ToList();
            if (!string.IsNullOrEmpty(request.TasksCode))
            {
                list = list.Where(x => x.TasksCode.Contains(request.TasksCode)).ToList();
            }

            if (!string.IsNullOrEmpty(request.TasksName))
            {
                list = list.Where(x => x.TasksName.Contains(request.TasksName)).ToList();
            }
            if (request.ChooseId > 0)
            {
                list = list.Where(x => x.ChooseId == request.ChooseId).ToList();
            }
            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = list;
                result.Msg = "显示成功";
            }

            return result;
        }
    }
}
